import psutil
import os
import tkinter as tk
from tkinter import messagebox


def judgeprocess(processname):
    global stext
    pl = psutil.pids()
    for pid in pl:
        if psutil.Process(pid).name() == processname:
            print(pid)
            stext.insert('insert',pid)
            stext.insert('insert','\n')
            return pid
            break
    else:
        print("not found")
        return 0
        
def file_name():
    name=os.path.basename(__file__)
    listname=name.split('.')
    f_name=listname[0]+'.exe'
    print(f_name)
    return f_name


##def get_pid():
##    pid=os.getpid()
##    return pid

def file_w(text):
    path=os.path.dirname(os.path.realpath(__file__))
    pah=path+'/name.log'
    with open(pah,'w') as f:
        f.write(str(text))

def file_r():
    path=os.path.dirname(os.path.realpath(__file__))
    pah=path+'/name.log'
    try:
        if os.path.exists(pah):
            with open(pah,'r') as f:
                pid=f.read()
                return pid
    except:
        return 0
    
def app_gui():
    global stext
    win=tk.Tk()
    win.title('process')
    win.geometry('600x600')
    btn=tk.Button(win,text='processid',command=lambda:judgeprocess(file_name()))
    btn.pack()
    stext=tk.Text(win)
    stext.pack()
    
    if file_r()==str(judgeprocess(file_name())):
        messagebox.showinfo('tip','已打开')
        return
    file_w(judgeprocess(file_name()))
    win.mainloop()

app_gui()



            


